﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Data;
using System.Web.UI.WebControls;

public partial class Catalogs_MenuRights : SessionPageBase
{
	protected void Page_Load(object sender, EventArgs e)
	{
		CustomControlsNS.CustomButton cc = new CustomControlsNS.CustomButton(Resources.DatabaseStrings.btn_Save, Resources.DatabaseStrings.btn_Save, "$get('" + btnSave.ClientID + "').click();", "1", true);
		cc.ID = "btnSave";
		this.Master.PageToolbarButtons.Add(cc);

		btnClose.OnClick = "$get('" + CancelButton1.ClientID + "').click();";

		if (!IsPostBack)
		{
			ClearSession("ds_MenuRigths");
			ClearSession("ds_DocTypes");
			ClearSession("ds_Users");
		}
		Load_Data(false);
	}

	private void Load_Data(bool bForce)
	{
		DataSet ds_MenuRigths = GetSessionDataSet("ds_MenuRigths");
		if (ds_MenuRigths == null || bForce)
		{
			ds_MenuRigths = Hipocrate.DB.DBConnection.GetData("Rights_Select", new string[] { }, new object[] { });

			foreach (DataTable dt in ds_MenuRigths.Tables)
				foreach (DataColumn dc in dt.Columns)
				{
					dc.ReadOnly = false;
					dc.AllowDBNull = true;
				}

			SetSession("ds_MenuRigths", ds_MenuRigths);
		}
		if (ds_MenuRigths != null && (!IsPostBack || bForce))
		{
			DataView dv0 = ds_MenuRigths.Tables[0].DefaultView;

			grStations.SessionKey = GetSessionKey("ds_MenuRigths");
			grStations.SessionKeyIndex = 0;
			grStations.SessionKeyFilter = dv0.RowFilter;
			grStations.TranslateColumns = new string[] { "Doc_Name" };

			grStations.DataSource = dv0;
			grStations.DataBind();
			grStations.CaptionForXLS = Resources.DatabaseStrings.menu_MenuRights;
		}
	}

	protected void btnSave_Click(object sender, EventArgs e)
	{
		try
		{
			DataSet ds_MenuRigths = GetSessionDataSet("ds_MenuRigths");
			if (ds_MenuRigths != null)
			{
				Hipocrate.DB.DBConnection.SaveAll(ds_MenuRigths,
					new object[] { 
						new object[] { "RightsGroups_Insert", new string[] { "DocTypes", "User_Group_ID", "Doc_Name", "User_Group" } }
					},
					new object[] { 
						new object[] { "RightsGroups_Update", new string[] { "ID", "DocTypes", "User_Group_ID" } }
					},
					new object[] { 
						new object[] { "RightsGroups_Delete", new string[] { "ID" } }
					}
					);
				this.Master.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
				Load_Data(true);
			}
		}
		catch (Exception ex)
		{
			this.Master.SetMessage(ex.Message, true);
		}
	}

	protected void dvUsersDetail_DataBound(object sender, EventArgs e)
	{
		DropDownList ddlDoc = (DropDownList)dvUsersDetail.Rows[1].FindControl("ddlDoc");
		DropDownList ddlUser = (DropDownList)dvUsersDetail.Rows[2].FindControl("ddlUser");

		DataSet ds_DocTypes = GetSessionDataSet("ds_DocTypes");
		if (ds_DocTypes == null)
		{
            ds_DocTypes = Hipocrate.DB.DBConnection.GetData("DocTypes_Select", new string[] { }, new object[] { });
			SetSession("ds_DocTypes", ds_DocTypes);
		}
		if (ds_DocTypes != null)
		{
			ddlDoc.DataSource = ds_DocTypes.Tables[0].DefaultView;
			ddlDoc.DataValueField = "ID";
			ddlDoc.DataTextField = "Name";
			ddlDoc.DataBind();
			ddlDoc.Items.Insert(0, new ListItem("", ""));
		}
		if (dvUsersDetail.DataItem != null)
			ddlDoc.SelectedValue = ((DataRowView)dvUsersDetail.DataItem)["DocTypes"].ToString();

		DataSet ds_Users = GetSessionDataSet("ds_Users");
		if (ds_Users == null)
		{
            ds_Users = Hipocrate.DB.DBConnection.GetData("UsersGroups_Select", new string[] { }, new object[] { });
			SetSession("ds_Users", ds_Users);
		}
		if (ds_Users != null)
		{
			ddlUser.DataSource = ds_Users.Tables[0].DefaultView;
			ddlUser.DataValueField = "ID";
			ddlUser.DataTextField = "Name";
			ddlUser.DataBind();
			ddlUser.Items.Insert(0, new ListItem("", ""));
		}
		if (dvUsersDetail.DataItem != null)
			ddlUser.SelectedValue = ((DataRowView)dvUsersDetail.DataItem)["User_Group_ID"].ToString();
	}

	protected void btnEdit_Click(object sender, EventArgs e)
	{
		dvUsersDetail.ChangeMode(DetailsViewMode.Edit);

		ImageButton btnDetails = sender as ImageButton;
		GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
		DataView dv = new DataView((GetSessionDataSet("ds_MenuRigths")).Tables[0]);
		dv.RowFilter = "ID = " + Convert.ToString(this.grStations.DataKeys[row.RowIndex].Value);

		this.dvUsersDetail.DataSource = dv;
		this.dvUsersDetail.DataBind();

		btnAdd.Visible = false;
		btnSave2.Visible = true;
		btnDelete.Visible = true;
		up2.Update();
		mdlPopup.Show();
		lblUser.Text = null;
	}

	protected void btnInsert_Click(object sender, EventArgs e)
	{
		dvUsersDetail.ChangeMode(DetailsViewMode.Insert);

		DataView dv = new DataView((GetSessionDataSet("ds_MenuRigths")).Tables[0]);
		this.dvUsersDetail.DataSource = dv;
		this.dvUsersDetail.DataBind();

		dvUsersDetail.Rows[0].Visible = false;

		btnAdd.Visible = true;
		btnSave2.Visible = false;
		btnDelete.Visible = false;
		up2.Update();
		mdlPopup.Show();
		lblUser.Text = null;
	}

	protected void btnSave2_Click(object sender, EventArgs e)
	{
		if (((DropDownList)dvUsersDetail.Rows[1].FindControl("ddlDoc")).SelectedItem.Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_user;
			return;
		}
		if (((DropDownList)dvUsersDetail.Rows[2].FindControl("ddlUser")).SelectedItem.Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_user;
			return;
		}
		string id = dvUsersDetail.DataKey.Value.ToString();
		DataView dv = new DataView((GetSessionDataSet("ds_MenuRigths")).Tables[0]);
		dv.RowFilter = "ID=" + id;

		dv[0]["DocTypes"] = Tools.GetSQLValue(((DropDownList)dvUsersDetail.Rows[1].FindControl("ddlDoc")).SelectedValue);
		dv[0]["Doc_Name"] = Tools.GetSQLValue(((DropDownList)dvUsersDetail.Rows[1].FindControl("ddlDoc")).SelectedItem.Text);
		dv[0]["User_Group_ID"] = Tools.GetSQLValue(((DropDownList)dvUsersDetail.Rows[2].FindControl("ddlUser")).SelectedValue);
		dv[0]["User_Group"] = Tools.GetSQLValue(((DropDownList)dvUsersDetail.Rows[2].FindControl("ddlUser")).SelectedItem.Text);

		grStations.DataBind();
		mdlPopup.Hide();
	}

	protected void btnAdd_Click(object sender, EventArgs e)
	{
		if (((DropDownList)dvUsersDetail.Rows[1].FindControl("ddlDoc")).SelectedItem.Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_user;
			return;
		}
		if (((DropDownList)dvUsersDetail.Rows[2].FindControl("ddlUser")).SelectedItem.Text == "")
		{
			lblUser.Text = Resources.DatabaseStrings.not_user;
			return;
		}

		DataView dv = new DataView((GetSessionDataSet("ds_MenuRigths")).Tables[0]);
		DataRowView drv = dv.AddNew();

		drv["DocTypes"] = Tools.GetSQLValue(((DropDownList)dvUsersDetail.Rows[1].FindControl("ddlDoc")).SelectedValue);
		drv["Doc_Name"] = Tools.GetSQLValue(((DropDownList)dvUsersDetail.Rows[1].FindControl("ddlDoc")).SelectedItem.Text);
		drv["User_Group_ID"] = Tools.GetSQLValue(((DropDownList)dvUsersDetail.Rows[2].FindControl("ddlUser")).SelectedValue);
		drv["User_Group"] = Tools.GetSQLValue(((DropDownList)dvUsersDetail.Rows[2].FindControl("ddlUser")).SelectedItem.Text);

		drv.EndEdit();

		grStations.DataBind();
		mdlPopup.Hide();
	}

	protected void btnDelete_Click(object sender, EventArgs e)
	{
		string id = dvUsersDetail.DataKey.Value.ToString();
		DataView dv = new DataView((GetSessionDataSet("ds_MenuRigths")).Tables[0]);
		dv.RowFilter = "ID=" + id;
		dv[0].Row.Delete();

		grStations.DataBind();
		mdlPopup.Hide();
	}

}
